package org.tloss.cluster;

import java.io.IOException;
import java.io.StringReader;
import java.net.URLDecoder;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class UpdateSessionFilter implements Filter {
	JAXBContext jaxbContext = null;
	Unmarshaller jaxbUnmarshaller = null;
	Marshaller jaxbMarshaller = null;

	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		HttpSession session = httpServletRequest.getSession(true);

		if (session != null) {
			if (Constants.SERVER_REQUEST_VALUE.equals(httpServletRequest
					.getHeader(Constants.SERVER_REQUEST))) {
				httpServletRequest = new HttpServletRequestWrapper(
						httpServletRequest, httpServletResponse);
				httpServletResponse.setHeader(Constants.REMOTE_SESSION_ID,
						session.getId());
			}
			Enumeration enumeration = httpServletRequest
					.getHeaders(Constants.ADD_SESSION);
			String tmp;
			SessionMeta obj;
			if (enumeration != null) {
				while (enumeration.hasMoreElements()) {
					tmp = (String) enumeration.nextElement();

					try {
						obj = (SessionMeta) jaxbUnmarshaller
								.unmarshal(new StringReader(tmp));
						JAXBContext context = JAXBContext.newInstance(Class
								.forName(obj.getSessionValueClass()));
						session.setAttribute(
								obj.getSessionKey(),
								context.createUnmarshaller()
										.unmarshal(
												new StringReader(obj
														.getSessionValue())));
					} catch (ClassNotFoundException e) {
					} catch (JAXBException e) {
					}
				}
			}
			enumeration = httpServletRequest
					.getHeaders(Constants.DELETE_SESSION);
			String[] tmpArr;
			if (enumeration != null) {
				while (enumeration.hasMoreElements()) {
					tmp = (String) enumeration.nextElement();
					if (tmp != null) {
						tmpArr = tmp.split("=");
						if (tmpArr != null && tmpArr.length == 2) {
							session.removeAttribute(URLDecoder.decode(
									tmpArr[1], "UTF-8"));
						}
					}
				}
			}
		}
		chain.doFilter(httpServletRequest, httpServletResponse);
	}

	public void init(FilterConfig arg0) throws ServletException {
		try {
			jaxbContext = JAXBContext.newInstance(SessionMeta.class);
			jaxbUnmarshaller = jaxbContext.createUnmarshaller();
			jaxbMarshaller = jaxbContext.createMarshaller();
			jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT,
					Boolean.TRUE);
			arg0.getServletContext().setAttribute(Constants.MARSHALLER,
					jaxbMarshaller);
		} catch (JAXBException e) {
			throw new ServletException(e);
		}
	}

}
